package com.sengled.cloud.biz.union.mgt.dao.sys;


import com.sengled.cloud.biz.union.mgt.model.po.Module;
import com.sengled.cloud.biz.union.mgt.model.po.ModulePermission;
import com.sengled.cloud.biz.union.mgt.model.vo.ModulePermissions;
import com.sengled.cloud.biz.union.mgt.model.vo.ModulePermissionsWithChecked;
import com.sengled.cloud.biz.union.mgt.model.vo.ModuleWithChecked;
import com.sengled.cloud.biz.union.mgt.model.vo.RoleInputTree;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ModuleDao {
    int deleteByPrimaryKey(Long id);

    int insert(Module record);

    int insertSelective(Module record);

    Module selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(Module record);

    int updateByPrimaryKey(Module record);

    @Select("SELECT id as id,module_code as moduleCode,module_name as moduleName,channel as channel  FROM module WHERE seal_mark = 0 and parent_id = #{parentId}")
    @Results({@Result(property = "id", column = "id"),
            @Result(property = "permissionList", javaType = List.class, column = "id",
                    many = @Many(select = "com.sengled.cloud.biz.union.mgt.dao.sys.ModulePermissionDao.selectByModuleKey"))})
    List<ModulePermissions> selectByParentId(Long parentId);

    @Select("SELECT id as id,module_code as moduleCode,module_name as moduleName,channel as channel FROM module WHERE seal_mark = 0 and parent_id = #{parentId}")
    @Results({@Result(property = "id", column = "id"),
            @Result(property = "permissionWithCheckedList", javaType = List.class, column = "id",
                    many = @Many(select = "com.sengled.cloud.biz.union.mgt.dao.sys.ModulePermissionDao.selectByModuleKey"))})
    List<ModulePermissionsWithChecked> selectByParentIdWithChecked(Long parentId);

    /*@Select("SELECT id as id,module_code as moduleCode,module_name as moduleName,channel as channel  FROM module WHERE id = #{parentId}")
    @Results({@Result(property = "id", column = "id"),
            @Result(property = "permissionList", javaType = List.class, column = "id",
                    many = @Many(select = "com.sengled.cloud.biz.union.mgt.dao.sys.ModulePermissionDao.selectByModuleKey"))})
    ModulePermissions selectByParentId1(Long parentId);*/

    @Select("<script>"
            + "SELECT id as id,module_code as moduleCode,module_name as moduleName,channel as channel,icon as icon,module_url as moduleUrl FROM module WHERE seal_mark = 0 and parent_id = #{parentId} and id IN "
            + "<foreach item='item' index='index' collection='moduleIds' open='(' separator=',' close=')'>"
            + "#{item}"
            + "</foreach>"
            + "</script>")
    List<ModulePermissions> selectByModuleKeys(@Param("moduleIds") List<Long> moduleIds,@Param("parentId") Long parentId);

    @Select("<script>"
            + "SELECT id as id,module_code as moduleCode,module_name as moduleName,channel as channel,icon as icon,module_url as moduleUrl FROM module WHERE seal_mark = 0 and id IN "
            + "<foreach item='item' index='index' collection='moduleIds' open='(' separator=',' close=')'>"
            + "#{item}"
            + "</foreach>"
            + "</script>")
    List<Module> selectByKeys(@Param("moduleIds") List<Long> moduleIds);

    @Select("select id as id,module_code as moduleCode,module_name as moduleName,parent_id as parentId,level as level,is_leaf as isLeaf,order_no as orderNo from module where seal_mark = 0 and channel = 'app' and parent_id = #{parentId}")
    List<ModuleWithChecked> selectPadModule(@Param("parentId") Long parentId);

    @Select("select DISTINCT m.id,m.module_code as moduleCode from module m,role_module_permission rmp,account_role a where m.seal_mark = 0 and a.user_account_id = #{accountId} and a.role_id = rmp.role_id and m.id = rmp.module_id and m.channel = 'APP' and rmp.enable_mark = 1 and m.parent_id = 0")
    List<Module> selectAppMainModule(@Param("accountId") Long accountId);

    @Select(" select id from module where seal_mark = 0 and module_code = #{moduleCode} ")
    Long selectIdByCode(@Param("moduleCode") String moduleCode);

    @Select("<script>"
            + "SELECT parent_id as parentId FROM module WHERE seal_mark = 0 and id IN "
            + "<foreach item='item' index='index' collection='moduleIds' open='(' separator=',' close=')'>"
            + "#{item}"
            + "</foreach>"
            + "</script>")
    List<Long> selectParentIdByModuleId(@Param("moduleIds") List<Long> moduleIds);
}